<!--
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-07-19 09:42:10
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-11-14 09:14:26
 * @Description:
 *
-->

<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="content">
        <div class="title">您好，欢迎登录</div>
        <div class="subtitle">算网一体编排调度平台</div>
        <!-- 用户名密码表单 -->
        <el-form
          ref="loginForm"
          class="login-form"
          :rules="rules"
          label-position="left"
          :model="loginForm"
        >
          <!-- 用户名 -->
          <el-form-item label="" prop="username">
            <div class="form-item-content">
              <img src="@/assets/imgs/login/icon_username.png" alt="" />
              <el-input
                v-model="loginForm.username"
                class="form-item-input"
                type="text"
                placeholder="请输入用户名"
                clearable
              />
            </div>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="" prop="password">
            <div class="form-item-content">
              <img src="@/assets/imgs/login/icon_password.png" alt="" />
              <el-input
                v-model="loginForm.password"
                class="form-item-input"
                type="password"
                show-password
                placeholder="请输入密码"
                clearable
                @keyup.enter.native="loginClick"
              />
            </div>
          </el-form-item>
        </el-form>
        <!-- 登录 -->
        <el-button
          class="login-btn"
          type="primary"
          :loading="loginLoading"
          @click="loginClick"
        >登 录</el-button>
        <!-- 去注册 -->
        <div class="register-btn">
          没有账号？<span @click="toRegister">点此注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 登录表单
      loginForm: {
        username: '', // 用户名
        password: '' // 密码
      },
      loginLoading: false, // 登录按钮loading
      redirect: undefined, // 登录完成重定向
      otherQuery: {}, // 重定向参数
      // 表单规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ], // 用户名
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' } // 密码
        ]
      }
    }
  },
  watch: {
    /**
     * @description: 监听route变化
     * @return {*}
     */
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect === '/login' ? '/' : query.redirect // 获取redirect
          this.otherQuery = this.getOtherQuery(query) // 获取额外参数
        }
      },
      immediate: true
    }
  },
  created() {},
  methods: {
    /**
     * @description: 去注册页面
     * @return {*}
     */
    toRegister() {
      // 跳转到注册
      this.$router.replace({
        path: '/register', // 路径
        query: this.$route.query // 携带query
      })
    },
    /**
     * @description: 登录
     * @return {*}
     */
    loginClick() {
      // 先校验
      this.$refs['loginForm'].validate((valid, obj) => {
        if (valid) {
          // 校验通过
          this.loginLoading = true
          // 调用store中的登录方法
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              // syncCloudVendors
              this.$store.dispatch('app/syncCloudVendors')
              // redirect
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery // 携带query
              })
              this.loginLoading = false
            })
            .catch(() => {
              this.loginLoading = false
            })
        }
      })
    },
    /**
     * @description: 获取参数
     * @param {*} query
     * @return {*}
     */
    getOtherQuery(query) {
      // reduce函数循环获取参数
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="less" scoped>
@import "@/assets/css/common.less";
.login-container {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url("~@/assets/imgs/bg_login.png") no-repeat center;
  background-size: cover;

  // 登录框
  .login-wrapper {
    position: absolute;
    top: 50%;
    right: 9.1%;
    transform: translateY(-50%);
    min-width: 480px;
    min-height: 500px;
    width: 28%;
    height: 57%;
    box-sizing: border-box;
    background: linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.1) 0%,
      rgba(255, 255, 255, 0.05) 100%
    );
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 7px;
    display: flex;
    align-items: center;
    justify-content: center;

    .content {
      width: 75%;
      .title {
        .textStyle(24px , 400, #F8F8F8);
        line-height: 33px;
        letter-spacing: 1px;
        margin-bottom: 10px;
        text-align: center;
      }
      .subtitle {
        .textStyle(37px, 600, #ffffff);
        line-height: 56px;
        letter-spacing: 1px;
        background: linear-gradient(180deg, #00eaff 0%, #009fff 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        margin-bottom: 40px;
        text-align: center;
      }

      // 登录表单
      .login-form {
        .el-form-item {
          margin-bottom: 25px;

          // 修改el-form表单item中label相关样式
          /deep/ .el-form-item__label {
            .textStyle(14px, 500, #B0BBCB);
            line-height: 20px;
            height: 20px;
            margin-bottom: 5px;
          }

          .form-item-content {
            height: 42px;
            background: rgba(109, 131, 174, 0.2);
            border-radius: 1px;
            border: 1px solid rgba(164, 183, 209, 0.4);
            display: flex;
            align-items: center;
            box-sizing: border-box;
            padding: 0 18px 0 24px;

            img {
              height: 16px;
              margin-right: 15px;
            }

            .form-item-input {
              box-sizing: border-box;

              // 修改el-form表单中input相关样式
              /deep/ .el-input__inner {
                padding-left: 0px;
                width: 100%;
                height: 100%;
                background-color: transparent;
                border: none;
                .textStyle(16px, 400, #ffffff);

                &::placeholder {
                  .textStyle(16px, 400, #A9B9CD);
                }
              }
            }
          }
        }
      }

      // 登录按钮
      .login-btn {
        width: 100%;
        height: 50px;
        background: linear-gradient(270deg, #00c0ff 0%, #338afb 100%);
        border-radius: 2px;
        border: none;
        .textStyle(21px, 400, #FFFFFF);
        margin-top: 20px;
      }

      // 注册按钮
      .register-btn {
        text-align: center;
        margin-top: 15px;
        .textStyle(16px, 500, #C3CDD9);
        line-height: 22px;

        span {
          color: #27b2ff;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
